//------------------------------------------------------------------------------------
#include <QFile>
#include <QtDebug>
#include "CMap.h"
//------------------------------------------------------------------------------------
#define MUR_CHAR            '#'
#define SNAKE_CHAR          '@'
#define CR					'\n'
//------------------------------------------------------------------------------------
CMap::CMap(void) : CSprite() {
    mur=new CMur();
	sol=new CSol();
}
//------------------------------------------------------------------------------------
CMap::~CMap(void) {
    delete mur;
	delete sol;
}
//------------------------------------------------------------------------------------
void CMap::load(QString ressourceName) {
    QFile res(ressourceName);
    res.open(QIODevice::ReadOnly|QIODevice::Text);
    QByteArray datas = res.readAll();
	int x, y;
	int i;

	memset(map, 0, NB_CELL_WIDTH*NB_CELL_HEIGHT*sizeof(bool));
	
	x = y = i = 0;
	while(i < datas.size()) {
		char c=datas.at(i);
		
		switch(c) {
			case MUR_CHAR:
				map[y*NB_CELL_WIDTH+x]=true;
				break;
			case SNAKE_CHAR:
				defaultSnakePos = QPoint(x, y);
				break;
			case CR:
				y++;
				x=-1;
				break;
			default:
				break;
		}
        i++;
		x++;
    }
}
//------------------------------------------------------------------------------------
void CMap::draw(QPainter *painter, int idxImage) {
    int x, y;
    
    for(y=0;y<NB_CELL_HEIGHT;y++) {
		for(x=0;x<NB_CELL_WIDTH;x++) {
            if(map[y*NB_CELL_WIDTH+x]) {
                mur->setPosition(QPoint(x, y));
                mur->draw(painter, idxImage % mur->getNbImage());
            }else {
                sol->setPosition(QPoint(x, y));
                sol->draw(painter, idxImage % sol->getNbImage());
			}
        }
    }
}
//------------------------------------------------------------------------------------

